package com.example.http.req;

import com.alibaba.fastjson.JSON;
import com.example.http.req.raw.FileFormDataEntity;
import com.example.http.req.raw.RawArrayEntity;
import com.example.http.req.raw.RawEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhouxl
 * @DATE 2021/8/10
 * @descriptoin
 */
@RestController
@RequestMapping("/api/formdata")
public class FormDataRequestController {

    /**
     * postman测试
     * urlencode post 提交方式可以获取提交的信息
     * urlencode get  无法获取提交的信息
     * 请求的参数信息
     *  name:1231
     *  password:123132
     *  rawInner.nickname:123123123
     *  rawInner.address:123123123
     * @param rawEntity
     * @return
     */
    @RequestMapping("/putInfo")
    public Object putInfo(RawEntity rawEntity){
        return JSON.toJSONString(rawEntity);
    }

    /**
     * jsonp调用方式
     * @param rawEntity
     * @return
     */
    @RequestMapping("jsonp")
    public Object jsonp(RawEntity rawEntity){
//        return JSON.toJSONString(rawEntity);
        return "callbackSuccess (" +JSON.toJSONString(rawEntity)+ ")";
    }

    /**
     * jsonp带有cookie信息
     * 在ajax设置
     *  header{
     *      Cookie:""
     *  }
     *  这样子的设置是无效的
     * @param rawEntity
     * @return
     */
    @RequestMapping("/jsonpCookie")
    public Object jsonpCookie(RawEntity rawEntity, HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() +"-->" + cookie.getValue());
        }
        return "callbackSuccess (" +JSON.toJSONString(rawEntity)+ ")";
    }

    /**
     * 带有文件的表单上传处理
     * 这种请求方式对应的是 multipart/form-data
     * @param entity
     * @return
     */
    @RequestMapping(value = "/putFile",method = RequestMethod.POST)
    public String putFile(FileFormDataEntity entity){
        return "success";
    }

    /**
     * 带有多个文件的表单上传处理
     * 这种请求方式对应的是 multipart/form-data
     * @param entity
     * @return
     */
    @RequestMapping(value = "/putFiles",method = RequestMethod.POST)
    public String putFiles(FileFormDataEntity entity){
        return "success";
    }

    /**
     * raw参数带有数组测试请求
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "putRawArrays",method = RequestMethod.POST)
    public String putRawArrays(@RequestBody RawArrayEntity entity){
        return JSON.toJSONString(entity);
    }

    /**
     * form-data传入数组参数
     * @param entity
     * @return
     */
    @RequestMapping("putFormDataArrays")
    public String putFormDataArrays(RawArrayEntity entity){
        return JSON.toJSONString(entity);
    }

}
